package cn.moukou.tongji.ihealth.doctor.service;

import cn.moukou.tongji.ihealth.core.bean.Resp;
import cn.moukou.tongji.ihealth.doctor.dao.CaseRecordMapper;
import cn.moukou.tongji.ihealth.doctor.pojo.CaseRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class CaseRecordService {

    @Autowired
    CaseRecordMapper caseRecordMapper;

    public int addOneCaseRecord(String pid, String patientName, int patientAge, String patientSex, String patientPhoneNumber,
                                String did, String doctorName,
                                String caseDescription, String solutionDescription) {
        Date date = new Date();
        CaseRecord caseRecord = new CaseRecord();

        caseRecord.setPid(pid);
        caseRecord.setPatientName(patientName);
        caseRecord.setPatientAge(patientAge);
        caseRecord.setPatientSex(patientSex);
        caseRecord.setPatientPhoneNumber(patientPhoneNumber);

        caseRecord.setDid(did);
        caseRecord.setDoctorName(doctorName);

        caseRecord.setCaseDescription(caseDescription);
        caseRecord.setSolutionDescription(solutionDescription);

        caseRecord.setTime(new Timestamp(date.getTime()));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        caseRecord.setTimestr(sdf.format(caseRecord.getTime()));

        return caseRecordMapper.insert(caseRecord);
    }

    public ArrayList<CaseRecord> getCaseRecordsByPid(String pid) {

        Map<String, Object> map = new HashMap<>();
        map.put("pid", pid);
        return (ArrayList<CaseRecord>) caseRecordMapper.selectByMap(map);
    }

    public CaseRecord getOneCaseRecord(Long id) {
        return caseRecordMapper.selectById(id);
    }

    public int updateOneCaseRecord(Long id,
                                   String pid, String patientName, int patientAge, String patientSex, String patientPhoneNumber,
                                   String caseDescription, String solutionDescription) {
        CaseRecord caseRecord = new CaseRecord();
        caseRecord.setId(id);
        caseRecord.setPid(pid);
        caseRecord.setPatientName(patientName);
        caseRecord.setPatientAge(patientAge);
        caseRecord.setPatientSex(patientSex);
        caseRecord.setPatientPhoneNumber(patientPhoneNumber);
        caseRecord.setCaseDescription(caseDescription);
        caseRecord.setSolutionDescription(solutionDescription);
        return caseRecordMapper.updateById(caseRecord);
    }

}
